<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <div class="conents">
          <el-tabs v-model="activeName">
            <el-tab-pane label="用户管理" name="second" />
          </el-tabs>
          <div class="el-rowconent">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px"
            >
              <el-col :span="20">
                <svg-icon icon-class="home" /><span>{{ companyName }}</span>
              </el-col>
              <el-col :span="2">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>负责人</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <el-dropdown-menu slot="dropdown">
                        <!-- 添加一级部门 pid 为 ''  -->
                        <el-dropdown-item
                          @click.native="hAdd('')"
                        >添加子部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
            <el-row type="flex">
              <el-divider />
            </el-row>
            <el-tree :data="list" :default-expand-all="true">
              <template v-slot="{ data }">
                <el-row
                  type="flex"
                  justify="space-between"
                  align="middle"
                  style="height: 40px; width: 100%"
                >
                  <el-col :span="20">
                    <span>{{ data.name }}</span>
                  </el-col>
                  <el-col :span="2">
                    <el-row type="flex" justify="end">
                      <!-- 两个内容 -->
                      <el-col>{{ data.manager }}</el-col>
                      <el-col>
                        <!-- 下拉菜单 element -->
                        <el-dropdown>
                          <span> 操作<i class="el-icon-arrow-down" /> </span>
                          <!-- 下拉菜单 -->
                          <el-dropdown-menu slot="dropdown">
                            <el-dropdown-item
                              @click.native="hAdd(data.id)"
                            >添加子部门</el-dropdown-item>
                            <el-dropdown-item
                              @click.native="hEdit(data.id)"
                            >编辑部门</el-dropdown-item>
                            <el-dropdown-item
                              @click.native="hDel(data.id)"
                            >删除部门</el-dropdown-item>
                          </el-dropdown-menu>
                        </el-dropdown>
                      </el-col>
                    </el-row>
                  </el-col>
                </el-row>
              </template>
            </el-tree>
          </div>
        </div>
      </el-card>
      <!-- 弹出层 -->
      <el-dialog
        :title="isEdit ? '编辑部门' : '添加部门'"
        :close-on-click-modal="false"
        :close-on-press-escape="true"
        :visible.sync="showDialog"
        @close="hDialogClose"
      >
        <addor-edit
          v-if="showDialog"
          :id="curId"
          ref="refDeptDialog"
          :is-edit="isEdit"
          :origin-list="originList"
          @close="hClose"
          @success="hSuccess"
        />
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { getDepartments, delDepartment } from '@/api/departments.js'
import { tranListToTreeData, newClassData, ToDoLostData } from '@/utils'
import AddorEdit from './deptDialog.vue'
export default {
  components: {
    AddorEdit
  },
  data() {
    return {
      activeName: 'second',
      companyName: '',
      list: [],
      showDialog: false, // 是否显示弹层
      curId: null,
      isEdit: false, // 是否处于编辑状态
      originList: []
    }
  },
  created() {
    this.loadDepartments()
  },
  methods: {
    // 获取信息
    async loadDepartments() {
      try {
        const { data } = await getDepartments()
        this.companyName = data.companyName
        // 由于业务中传智教育的pid是:-1，它是最高级的节点
        // shift 从数组头部取出一个元素
        const root = data.depts.shift()
        // 将需要表单验证的数据抽离出来 然后传给子组件
        this.originList = data.depts.map(({ id, pid, code, name }) => ({
          id,
          pid,
          code,
          name
        }))
        // console.log(data.depts)
        // 利用双循环查找
        this.list = ToDoLostData(data.depts, '')
        // console.log(this.list)
        // 递归查找法
        // this.list = newClassData(data.depts)
        this.list.unshift(root)
      } catch (error) {
        console.log(error)
      }
    },
    // 添加部门
    hAdd(id) {
      // 弹出层出来
      this.showDialog = true
      // 把 id 传给 子组件获取用户详情
      this.curId = id
      //  传递一个标识给子组件，告诉它我们现在是添加状态
      this.isEdit = false
    },
    // 编辑部门
    hEdit(id) {
      // 弹出层出来
      this.showDialog = true
      // 把 id 传给 子组件获取用户详情
      this.curId = id
      //  传递一个标识给子组件，告诉它我们现在是编辑状态
      this.isEdit = true
    },
    // 删除部门
    async doDel(id) {
      try {
        // 发送请求删除信息
        await delDepartment(id)
        // 1. 提示
        this.$message({ type: 'success', message: '删除成功!' })
        // 重新发起获取部门基本信息请求
        this.loadDepartments()
      } catch (error) {
        this.$message({ type: 'error', message: '删除失败' })
      }
    },
    hDel(id) {
      this.$confirm('此操作将永久删除部门, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          // 删除
          this.doDel(id)
        })
        .catch(() => {})
    },
    // 关闭弹层，再次更新数据
    hSuccess() {
      this.showDialog = false
      this.loadDepartments()
    },
    // 收到子组件 hClose 事件
    hClose() {
      this.showDialog = false
      this.isEdit = false
    },
    // 在回调中，找到子组件，执行resetForm方法
    hDialogClose() {
      console.log('close')
      this.$refs.refDeptDialog.resetForm()
    }
  }
}
</script>
<style lang="scss">
.department-container {
  .el-dialog__body {
    padding: 0 !important;
  }
}
</style>
<style lang="scss" scoped>
.department-container {
  font-size: 14px;
  .el-rowconent {
    margin: 30px 120px 0;
    font-weight: 800;
  }
  .conents {
    margin-left: 20px;
    .el-tabs {
      margin: 20px 0 0 20px;
    }
  }
  .el-divider {
    margin: 8px 0 14px 0;
  }
  center {
    padding: 10px 20px 20px 20px;
  }
}
</style>
